package cn.ruituo.web;

import cn.ruituo.web.ctrl.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "MainServlet",value = "*.do")
public class MainServlet extends HttpServlet {
    private Map<String,Method> methodmap =new HashMap<>();
    private StudentController s=new StudentController();
    private SubjectController c=new SubjectController();
    private GradeController g=new GradeController();
    //请求地址是-隔开的，左边是属性，右边是方法，方法参数必须是response,request
    //   s-get.do
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path=req.getServletPath();

        path=path.substring(1,path.length()-3);

        String[]ar=path.split("-");
        //
        try {
            Field f = this.getClass().getDeclaredField(ar[0]);
            Object controller = f.get(this);//获得了属性
            Method method= methodmap.get(path);
            if(method==null){
                 synchronized (methodmap){
                     method = controller.getClass().getDeclaredMethod(ar[1], HttpServletRequest.class, HttpServletResponse.class);
                     methodmap.put(path,method);
                 }
            }

            Object o = method.invoke(controller, req, resp);
            if(o!=null && o instanceof String){
                req.getRequestDispatcher((String)o).forward(req,resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    //    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        String path=request.getServletPath();
//        PrintWriter out = response.getWriter();
//        out.print("<!DOCTYPE html>");
//        out.print("<html>");
//        out.print("<body>");
//
//        out.print("<body>");
//        out.print("</body>");
//        //和请求相关的request方法
//        out.print("<h1>上下文:"+request.getContextPath()+"</h1>");
//        out.print("<h1>URL:"+request.getRequestURL()+"</h1>");
//        out.print("<h1>URI:"+request.getRequestURI()+"</h1>");
//        out.print("<h1>请求地址:"+path+"</h1>");
//        path=path.substring(1,path.length()-3);
//        //if(path.equals("doGet"))
//        try {
//            Method m=this.getClass().getDeclaredMethod(path,HttpServletResponse.class,HttpServletRequest.class);
//            Object o=m.invoke(this,response,request);
//            out.print("<h1>返回地址:"+o+"</h1>");
////            if(o!=null && o instanceof String){
////                request.getRequestDispatcher((String)o).forward(request,response);
////            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            out.print("<h1>此方法没有实现</h1>");
//        }
//
////        if("add".equals(path))
////        {
////            out.print("增加操作");
////            add(request, response);
////        }else if("update".equals(path))
////        {
////            out.print("修改操作");
////            update(request, response);
////        }else if("login".equals(path))
////        {
////            out.print("修改操作");
////            login(request, response);
////        }else{
////            out.print("未知操作");
////        }
//        out.print("</html>");
//        //加一个参数表示行为
//        /*String op=request.getParameter("operate");
//        switch(op){
//            case "addSubject":{
//
//            }
//                break;
//        }*/
//    }

    public void add(  HttpServletResponse response,HttpServletRequest request) throws ServletException, IOException{
        PrintWriter writer = response.getWriter();
        writer.print("这是增加方法");
    }
    public void del( HttpServletResponse response,HttpServletRequest request) throws ServletException, IOException{
        PrintWriter writer = response.getWriter();
        writer.print("这是删除方法");
    }
    public void update( HttpServletResponse response,HttpServletRequest request) throws ServletException, IOException{
        PrintWriter writer = response.getWriter();
        writer.print("这是xiugai方法");
    }
    public void search( HttpServletResponse response,HttpServletRequest request) throws ServletException, IOException{
        PrintWriter writer = response.getWriter();
        writer.print("这是查询方法");
    }
    //返回的是视图的地址
    public String login( HttpServletResponse response,HttpServletRequest request) throws ServletException, IOException{
        PrintWriter writer = response.getWriter();
        writer.print("这是登录方法");
        String uname = request.getParameter("uname");
        String upass = request.getParameter("upass");
        if("java72".equals(uname)&&"123456".equals(upass)){
            writer.print("登录成功");
            return "/welcome.jsp";
        }
        return "login3.jsp";
    }
}
